/**CFile***********************************************************************
 
 FileName    [main.c]
 
 Synopsis    [Main function. Logic draws menu, handles input, and calls
	appropriate functions.]
 
 Author      [Aaron Stromberg, Hannah Smith, Julia Vidal Piñeiro, Nick Taglianetti]
 
 Copyright   [Copyright (c) 2012 Carlos III University of Madrid
 All rights reserved.
 
 Permission is hereby granted, without written agreement and without license
 or royalty fees, to use, copy, modify, and distribute this software and its
 documentation for any purpose, provided that the above copyright notice and
 the following two paragraphs appear in all copies of this software.
 
 IN NO EVENT SHALL THE CARLOS III UNIVERSITY OF MADRID BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
 OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE CARLOS III
 UNIVERSITY OF MADRID HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 THE CARLOS III UNIVERSITY OF MADRID SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN
 "AS IS" BASIS, AND CARLOS III UNIVERSITY OF MADRID HAS NO OBLIGATION TO
 PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]
 
 ******************************************************************************/
#ifdef DEBUG
#define DBUG 1
#else
#define DBUG 0
#endif

#include "biblio_reference.h"
#include "data_read.h"
#include "file_ops.h"
#include "hash_table.h"
#include <stdio.h>
#include <stdlib.h>


/**Function********************************************************************

  Main function where the program starts. Executes the option that the
  user chooses.
  
******************************************************************************/
int main()
{
    if (DBUG)
	printf("\n\tWelcome to DEBUG mode!\n");    
    
    unsigned int iSelection;   
    entry_t * hash_table[TABLE_SIZE] = {NULL};

    while (1)
    {       
        display_menu();        

        if(get_int(&iSelection)==-1)           
        {
            printf("ERROR");   
        }

        printf("%d", iSelection);
       
        if ( iSelection == 2 )           
        {
            publication_t *pub = new_pub();
            insert_entry(hash_table, pub);
            //printf("DEBUG: entry has been successfully added!");
        } 
        else if ( iSelection == 3 )
        {
            display_pubs(hash_table);
        }
        else if ( iSelection == 4 )   
        {
            const char *doi = get_doi();
            display_pub(hash_table, doi);
        }
        else if ( iSelection == 5 )
        {
             const char *doi = get_doi();
             modify_pub(hash_table, doi); 
        }
        
        else if ( iSelection == 6 )
        {
            const char *doi = get_doi();
            delete_pub(hash_table, doi);
        }
        else if ( iSelection == 7 )
        {
            export_to_file(hash_table);
        }
        else if ( iSelection == 8 )
        {
            import_from_file(hash_table);
        }
        else if ( iSelection == 9 )
        {
            printf("Enter starting year:\n");
            int start_year = atoi(read_text());
            sort_table(hash_table, start_year);      
        }
        else if ( iSelection == 1)   
            break;
    }
    
    // cleanup memory before exit    
    if (DBUG)
	printf("\nDEBUG: freeing table!\n");

    free_table(hash_table);

    if (DBUG)
        printf("\nDEBUG: everything freed!\n");
    
    return 1;
}

